<?php

namespace app\shop\controller\store;

use app\shop\model\store\CarfilmSolarProduct;
use app\shop\model\store\CarfilmBooking;
use app\shop\model\store\CarfilmWarranty;
use app\common\service\Result;
use app\shop\model\user\User;
use app\common\service\message\MessageService;
class Carfilm
{
    public function list(){
        $app_id = request()->app_id;
        $page_size = $data['page_size'] ?? 20;
        $lists = CarfilmSolarProduct::where("app_id",$app_id)->order('id desc')->paginate($page_size);
        return Result::success($lists);
    }

    public function category(){
        $app_id = request()->app_id;
        $res = CarfilmSolarProduct::where("app_id",$app_id)->column('category');
        $lists = [];
        foreach(array_unique($res) as $k=>$v){
            $lists[] = [
                'value'=>$k,
                'label'=>$v,
            ];
        }
        return Result::success($lists);
    }

    public function info(){
        $id = input('id');
        $info = CarfilmSolarProduct::field('id,category,part_name,price,period,status')->find($id);
        return Result::success($info);
    }

    public function add(){
        $data = input();
        unset($data['id']);
        $info = CarfilmSolarProduct::create($data);
        return Result::success($info);
    }

    public function edit(){
        $data = input();
        $id = $data['id']??0;
        $info = CarfilmSolarProduct::find($id);
        $info->save($data);
        return Result::success($info);
    }

    public function orderList(){
        $data = input();
        $map = [
            ['app_id','=',request()->app_id]
        ];
        $keyword = $data['keyword']??"";
        if($keyword){
            $map[] = ['owner_name|owner_phone|license_plate','like',"%$keyword%"];
        }
        $booking_region = $data['booking_region']??[];
        $booking_region = $booking_region[2]??0;
        if($booking_region){
            $map[] = ['booking_region','=',$booking_region];
        }
        /*$store_id = $data['store_id']??0;
        if($store_id>=0){
            $map[] = ['store_id','=',$store_id];
        }*/
        $status = $data['status']??"0";
        if($status=="0"){
            $map[] = ['store_id','=',0];
        }
        else{
            $map[] = ['store_id','>',0];
        }
        $model = CarfilmBooking::where($map);
        $create_at = $data['create_at']??[];
        if($create_at){
            $model->whereBetweenTime('created_at',$create_at[0],$create_at[1]);
        }
        $page_size = $data['page_size']??20;
        $lists = $model->with('store')->order('booking_id desc')->paginate($page_size);
        return Result::success($lists);
    }

    public function setStore(){
        $data = input();
        $booking = CarfilmBooking::find($data['booking_id']);
        if($booking->app_id!=request()->app_id){
            return Result::error("无权操作");
        }
        if($booking->store_id>0){
            return Result::error("已分配门店");
        }
        $booking->store_id = $data['store_id'];
        $booking->save();
        //发送订阅消息
        $user = User::find($data['store_id']);
        if($user->open_id){
            $param = [
                'app_id'=>$booking->app_id,
                'created_at'=>$booking->created_at,
                'remark'=>($booking->owner_name)."(".($booking->owner_phone).")",
                'openid'=>$user->open_id
            ];
            (new MessageService())->bookAlert($param);
        }
        return Result::success();
    }
    
    public function warrantyList(){
        $data = input();
        $map = [
            //['app_id','=',request()->app_id]
        ];
        $owner = $data['owner']??"";
        if($owner){
            $map[] = ['owner_name|owner_phone','like',"%$owner%"];
        }
        $license = $data['license']??"";
        if($license){
            $map[] = ['license_plate','like',"%$license"];
        }
        $store_user = $data['store_user']??0;
        if($store_user>=0){
            $map[] = ['store_user','=',$store_user];
        }
        $model = CarfilmWarranty::where($map);
        $construction_date = $data['construction_date']??[];
        if($construction_date){
            $model->whereBetweenTime('construction_date',$construction_date[0],$construction_date[1]);
        }
        $page_size = $data['page_size']??20;
        $lists = $model->with('store')->order('warranty_id desc')->paginate($page_size);
        return Result::success($lists);
    }
}
